package com.course.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.course.api.config.Result.Result;
import com.course.api.config.Result.Results;
import com.course.api.entity.Role;
import com.course.api.service.Impl.RoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleServiceImpl roleService;

    /**
     * 获取角色信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/getPage")
    public Result getPage(@RequestParam int page,
                          @RequestParam int pageSize,
                          @RequestParam(defaultValue = "") String name) {
        IPage<Role> roleIPage = new Page<>(page, pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name);
        return Results.success(roleService.page(roleIPage, queryWrapper));
    }

    /**
     * 添加角色信息
     * @param role
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Role role) {
        return Results.success(roleService.save(role));
    }

    /**
     * 编辑角色信息
     * @param role
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Role role) {
        return Results.success(roleService.updateById(role));
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result remove(@PathVariable(value = "id") Long id) {
        return Results.success(roleService.removeById(id));
    }

    /**
     * 获取角色权限信息
     * @param roleId
     * @return
     */
    @GetMapping("/getPermissionInfo/{roleId}")
    public Result getPermissionInfo(@PathVariable(value = "roleId") Long roleId) {
        return Results.success(roleService.getRoleMenu(roleId));
    }

    /**
     * 用户权限分配
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("/setRolePermission/{roleId}")
    public Result setRolePermission(@PathVariable(value = "roleId") Long roleId,
                                    @RequestBody List<Long> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return Results.success();
    }

}
